define(function (require, exports, module) {
	var Grid = require("grid");
	var store = require("store");
	var activeUser = store.get("user");
	var aw = require("ajaxwrapper")
    var instance = {
        initComponent: function (params,widget) {
        	var grid = new Grid({
                parentNode: ".J-grid", 
//                autoRender:false,
                model : {
                    url : "api/returncredentials/query",
                    params: function() {
                    	var subnav=widget.get("subnav");
                        return {
                        	"flowStatus":subnav.getValue("status"),
                        	fetchProperties:"*,travelContract.*,travelContract.member.personalInfo.*,travelContract.productRule.product.*,travelContract.productType.*"
                        };
                    },
                    columns : [{
        				name:"travelContract.memberCertificates",
        				label:"会员证号码"  ,
        				 format:"detail",
         			    formatparams: {
         			    	key:"detail",
         			    	handler:function(index,data,rowEle){
         			    		var form = widget.get("form");
								var memberCertificates = data.travelContract.memberCertificates;
								if(memberCertificates){
									widget.handler.form.searchContact(widget,memberCertificates,data.pkReturnCredentials,data);
								}
         			    	}
         			    }
					},{
        				name:"travelContract.contactNo",
        				label:"合同号"
        			},{
        				name:"travelContract.member.personalInfo.name",
        				label:"会员"        				
					},{
        				name:"travelContract.member.personalInfo.idNumber",
        				label:"证件号"
        			},{						
						name:"travelContract.productRule.product.name",
        				label:"产品",
					},{
        				name:"travelContract.productType.name",
        				label:"产品类型",
        			},{
						name:"travelContract.number",
						label:"份数"
					},{
						name:"operateDate",
						label:"申请日期",
						format:"date"
					},{
        				name:"price",
        				label:"应退金额（元）",
					},{
        				name:"flowStatus.value",
        				label:"状态",
        			},{
						name:"operate",
						label:"操作",
						format:"button",
						formatparams:[{
								key:"approval",
								text: "审批",
								handler:function(index,data,rowEle){
									widget.show([".J-form",".J-couponGrid",".J-approvalUI"]).hide([".J-grid"]);
		        					widget.get("subnav").hide(["search","status"]).show(["back"]);
		        					var form = widget.get("form");
		        					form.setValue("memberCertificates",data.travelContract.memberCertificates);
		        					form.setValue("contactNo",data.travelContract.contactNo);
		        					form.setValue("status.value",data.travelContract.status.value);
		        					form.setValue("product.name",data.travelContract.productRule.product.name);
		        					form.setValue("productType.name",data.travelContract.productType.name);
		        					form.setValue("number",data.travelContract.number);
		        					form.setValue("totalPrice",data.travelContract.price);
		        					form.setValue("signUpDate",data.travelContract.signUpDate);
		        					form.setValue("balance",data.travelContract.balance==null? 0.00 :data.travelContract.balance);
		        					form.setValue("name",data.travelContract.member.personalInfo.name);
		        					form.setValue("idNumber",data.travelContract.member.personalInfo.idNumber);
		        					form.setValue("mobilePhone",data.travelContract.member.personalInfo.mobilePhone==null?" ":data.travelContract.member.personalInfo.mobilePhone);
		        					form.setValue("defaultRatio",data.defaultRatio==null? 0.00:(data.defaultRatio).toFixed(2));
		        					form.setValue("flatCost",data.flatCost==null?0.00:(data.flatCost).toFixed(2));
		        					form.setValue("operateDate",data.operateDate);
		        					form.setValue("price",data.price==null?0.00:(data.price).toFixed(2));
		        					form.setValue("operate",activeUser.name); //d.operator.pkUser
		        					form.setValue("cause",data.cause);
		        					//消费券
		        					var memberCertificates = data.travelContract.memberCertificates;
									if(memberCertificates){
										widget.handler.grid.searchContact(widget,memberCertificates,data.pkReturnCredentials,data);
									}
		        					
									pkTravelContract = data.travelContract.pkTravelContract;
		        					widget.set("pkTravelContract",data.travelContract.pkTravelContract);
		        					Rcversion = data.version;
		        					widget.set("Rcversion",data.version);
		        					pkReturnCredentials = data.pkReturnCredentials;
		        					widget.set("pkReturnCredentials",data.pkReturnCredentials);
		        					cause = data.cause;
		        					widget.set("cause",cause);
		        					defaultRatio = data.defaultRatio;
		        					widget.set("defaultRatio",defaultRatio);
		        					flatCost = data.flatCost;
		        					widget.set("flatCost",flatCost);
		        					cause = data.cause;
		        					widget.set("cause",cause);
		        					operateDate = data.operateDate;
		        					widget.set("operateDate",operateDate);
		        					operator = data.operator.pkUser;
		        					widget.set("operator",operator);
		        					price = data.price;
		        					widget.set("price",price);
									var approvalUI  = widget.get("approvalUI");
									approvalUI.set("param",{
	    								modelId:data.pkReturnCredentials,
	    								serviceType:"ReturnCredentials",
	    								hideButton:false,
	    								callBack:function(data){
	    									widget.get("grid").refresh();
	    									var pkTravelContract=widget.get("pkTravelContract");
	    									var Rcversion = widget.get("Rcversion");
	    									var pkReturnCredentials = widget.get("pkReturnCredentials");
	    									var defaultRatio = widget.get("defaultRatio");
	    									var flatCost = widget.get("flatCost");
	    									var operateDate = widget.get("operateDate");
	    									var operator = widget.get("operator");
	    									var price = widget.get("price");
	    									var cause = widget.get("cause");	    								}
	    							});
									approvalUI.get("appgrid").refresh();
									
								}
						}]
					}]
                }
            });
            widget.set("grid",grid);
        }
    };
    module.exports = instance;
});